<?php

class PsplayoutsController extends AppController {
	public $name = 'psplayouts';
	
    public $pspLayout_out = null;	
    public $pspLayout_pares_data = array();
	
	public function beforeFilter() {
 	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('clearlayout','layoutupdata','editlayout','outaddlayout','inaddlayout','dellayout','addtags','uplayout','downlayout');
	}

	public function clearlayout() {	
		$startpos = Request::$query['startpos'];		
		$endspos = Request::$query['endspos'];		
		$endpos = Request::$query['endpos'];	
		$pagename = Request::$query['pagename'];	
		$weburl = Request::$query['weburl'];		
		$webroot = Request::$query['webroot'];		
		$act = Request::$query['act'];			
		$app = Request::$query['app'];		
		$compeld = Request::$query['compeld'];	

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);

		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";

		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;			
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($viewFileName) && @filemtime($viewFileName) > @filemtime($MviewFileName)) {
				$nowFileName = $viewFileName;		   
			}else if( file_exists($MviewFileName) && @filemtime($viewFileName) < @filemtime($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
        if(empty($layoutdata))	{
            $this->flash("至少要有一个layout标签存在!", Request::referer());
        }	
		$nowlayout = false;
        foreach($layoutdata as $key => $value) {
            if( $value['startpos']==$startpos && $value['endspos']==$endspos && $value['endpos']==$endpos ) {
			    $nowlayout = $key;
			}
        }		
        if($nowlayout===false) {
            $this->flash("该layout标签不存在!", Request::referer());	 
        }	
		$newlayout = "<psp:layout   width='100px' ></psp:layout>";
		$content = $this->delete_str($this->pspLayout_out, $endspos+1 ,$endpos-1 );
		if (!file_exists($MviewFileName)) {
			$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
			$temp = explode(DS, $MviewFileName);
			array_pop( $temp );
			$tempFileDir = implode( DS.'' , $temp );
			get_dir($tempFileDir);
		}		
		if (!file_put_contents($MviewFileName, $content)) {
			$this->flash("操作失败!", Request::referer());
		}		
		$this->flash("操作成功!", Request::referer());		
	}	
		
	
	
	
	public function layoutupdata() {
	
		$pagename = Request::get_args("pagename");	
		$weburl = Request::get_args("weburl");		
		$webroot = Request::get_args("webroot");		
		$act = Request::get_args("act");			
		$app = Request::get_args("app");		
		$compeld = Request::get_args("compeld");

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
        $urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";	
		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;		
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
		$toplayout = array();
		$toplayoutstr ='';
		$num=0;
		foreach($layoutdata as $key => $value) {
			if( $value['parentid']==0) {
				$toplayout[$num] = $key;
				$toplayoutstr .= $key.',';
				$num++;
			}
		}		

		$order = Request::get_args("order");
		$itemid = trim(trim(Request::get_args("id")),',');		
		if (!empty ($itemid)) {

			$newlayout = explode(",",$itemid);
			foreach($newlayout as $k => $v) {
				if(!in_array($v,$toplayout)){
					unset($newlayout[$k]);
				}
			}
			$movepos = 0;
			$afterpos =0;
			$beforepos =0;			
			$upordown = 0;//表示向上移动
			foreach($toplayout as $k =>$v) {	
			    if($newlayout[$k]!=$v){
				    if(isset($newlayout[$k+1]) && $newlayout[$k] > $newlayout[$k+1]) {
					    $upordown = 0;//表示向上移动
                        $movepos = $newlayout[$k];
						$afterpos = $newlayout[$k+1];//表示移动到的位置的下一个位置编号							
					}else {
					    $upordown = 1;//表示向下移动
                        $movepos = $v;						
						foreach($newlayout as $k1 =>$v1) {	
							if($v1==$movepos){
								$beforepos = $newlayout[$k1-1];//表示移动到的位置的上一个位置编号								
								break;
							}				
						}									
					}				   
					break;
				}				
			}	
			$content = $this->pspLayout_out;			
			$getlayouthtml = substr($content,$layoutdata[$movepos]['startpos'], 
				($layoutdata[$movepos]['endpos']+13)-$layoutdata[$movepos]['startpos'] ); 
				
            if($upordown==1) {//表示向下移动
				$content = $this->insert_str($content, $layoutdata[$beforepos]['endpos']+13, $getlayouthtml);				
				$content = $this->delete_str($content, $layoutdata[$movepos]['startpos'] ,$layoutdata[$movepos]['endpos']+12 );
			}else {//表示向上移动			
				$content = $this->delete_str($content, $layoutdata[$movepos]['startpos'] ,$layoutdata[$movepos]['endpos']+12 );		
				$content = $this->insert_str($content, $layoutdata[$afterpos]['startpos'], $getlayouthtml);				
			}

			$query = array('sortlist'=>array('sort'=>$itemid,'old'=>$toplayoutstr));        	
			$sql ='[@@:UM sortlist][@@:QU save]';
			POP::exe($sql,$query);	

			if (!file_exists($MviewFileName)) {
				$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
				$temp = explode(DS, $MviewFileName);
				array_pop( $temp );
				$tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
			}	
		
			if (!file_put_contents($MviewFileName, $content)) {
				echo "none";
			}else {
				echo $itemid;
			}

		}
		exit;
    }	

	
//http://localhost/psp_framework_gai_utf8_2013/index.php/psplayouts/layoutupdata/?weburl=http://localhost/psp_framework_gai_utf8_2013/index.php&compeld=D:\wamp1\www\psp_framework_gai_utf8_2013\app\temp\viewsmanages\app\default\default\index.ps&app=default&act=index&webroot=D:\wamp1\www\psp_framework_gai_utf8_2013\myhome\default&pagename=default\index.ps&order=1,2,3,4,5,6,7&id=1,2,7,3,4,5,6
	
	public function editlayout() {
	
		$startpos = Request::get_args("startpos");		
		$endspos = Request::get_args("endspos");		
		$endpos = Request::get_args("endpos");	
		$pagename = Request::get_args("pagename");	
		$weburl = Request::get_args("weburl");		
		$webroot = Request::get_args("webroot");		
		$act = Request::get_args("act");			
		$app = Request::get_args("app");		
		$compeld = Request::get_args("compeld");

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
        $urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";	
		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;			
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($viewFileName) && @filemtime($viewFileName) > @filemtime($MviewFileName)) {
				$nowFileName = $viewFileName;		   
			}else if( file_exists($MviewFileName) && @filemtime($viewFileName) < @filemtime($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
		
		if(empty($layoutdata))	{
			$this->flash("至少要有一个layout标签存在!", Request::referer());
		}	
		$nowlayout = false;
		foreach($layoutdata as $key => $value) {
			if( $value['startpos']==$startpos && $value['endspos']==$endspos && $value['endpos']==$endpos ) {
				$nowlayout = $key;
			}
		}		
		if($nowlayout===false) {
			$this->flash("该layout标签不存在!", Request::referer());	 
		}		
		
		
		$do = Request::get_args("do");		
	    if($do=='do') {
            $style = Request::get_args("style");
			$arrName = explode(";",$style); 
            $config = array();	
            $style = '';			
			for($i=0;$i<count($arrName);$i++) { 
				$TempTagName = $arrName[$i];  
				//根据=再次进行分割
				$arrParamter = explode(":",$TempTagName); 
				if(isset($arrParamter[0]) && isset($arrParamter[1]) && !empty($arrParamter[0]) && !empty($arrParamter[1])) {
					$style .= $arrParamter[0]."='".$arrParamter[1]."' ";
				}
			}

			$content = $this->pspLayout_out;		

			$content = $this->delete_str($content, $layoutdata[$nowlayout]['startpos'] ,$layoutdata[$nowlayout]['endspos'] );
			
			$content = $this->insert_str($content, $layoutdata[$nowlayout]['startpos'], '<psp:layout '.$style.'>');	
	
			if (!file_exists($MviewFileName)) {
				$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
				$temp = explode(DS, $MviewFileName);
				array_pop( $temp );
				$tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
			}		
			if (!file_put_contents($MviewFileName, $content)) {
				$this->flash("操作失败!", $fromurl);
			}		
			$this->flash("操作成功!", $fromurl);
        }

	    $this->set('weburl', $weburl);
	    $this->set('fromurl', $fromurl);	

	    $this->set('compeld', $compeld);
	    $this->set('webroot', $webroot);
	    $this->set('app', $app);
	    $this->set('act', $act);	
        $this->set('pagename', $pagename);	

        $this->set('startpos', $startpos);
        $this->set('endspos', $endspos);		
        $this->set('endpos', $endpos);

        $this->set('data', $layoutdata[$nowlayout]['style']);
	
	}		
	
	
	public function outaddlayout() {	
		$startpos = Request::$query['startpos'];		
		$endspos = Request::$query['endspos'];		
		$endpos = Request::$query['endpos'];	
		$pagename = Request::$query['pagename'];	
		$weburl = Request::$query['weburl'];		
		$webroot = Request::$query['webroot'];		
		$act = Request::$query['act'];			
		$app = Request::$query['app'];		
		$compeld = Request::$query['compeld'];	

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);

		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";

		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;			
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($viewFileName) && @filemtime($viewFileName) > @filemtime($MviewFileName)) {
				$nowFileName = $viewFileName;		   
			}else if( file_exists($MviewFileName) && @filemtime($viewFileName) < @filemtime($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
        if(empty($layoutdata))	{
            $this->flash("至少要有一个layout标签存在!", Request::referer());
        }	
		$nowlayout = false;
        foreach($layoutdata as $key => $value) {
            if( $value['startpos']==$startpos && $value['endspos']==$endspos && $value['endpos']==$endpos ) {
			    $nowlayout = $key;
			}
        }		
        if($nowlayout===false) {
            $this->flash("该layout标签不存在!", Request::referer());	 
        }	
		$newlayout = "<psp:layout   width='100px' ></psp:layout>";
		$content = $this->insert_str($this->pspLayout_out, $endpos+13, $newlayout);

		if (!file_exists($MviewFileName)) {
			$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
			$temp = explode(DS, $MviewFileName);
			array_pop( $temp );
			$tempFileDir = implode( DS.'' , $temp );
			get_dir($tempFileDir);
		}		
		if (!file_put_contents($MviewFileName, $content)) {
			$this->flash("操作失败!", Request::referer());
		}		
		$this->flash("操作成功!", Request::referer());		
	}

	public function inaddlayout() {	
		$startpos = Request::$query['startpos'];		
		$endspos = Request::$query['endspos'];		
		$endpos = Request::$query['endpos'];	
		$pagename = Request::$query['pagename'];	
		$weburl = Request::$query['weburl'];		
		$webroot = Request::$query['webroot'];		
		$act = Request::$query['act'];			
		$app = Request::$query['app'];		
		$compeld = Request::$query['compeld'];	

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);

		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";

		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;			
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($viewFileName) && @filemtime($viewFileName) > @filemtime($MviewFileName)) {
				$nowFileName = $viewFileName;		   
			}else if( file_exists($MviewFileName) && @filemtime($viewFileName) < @filemtime($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
        if(empty($layoutdata))	{
            $this->flash("至少要有一个layout标签存在!", Request::referer());
        }	
		$nowlayout = false;
        foreach($layoutdata as $key => $value) {
            if( $value['startpos']==$startpos && $value['endspos']==$endspos && $value['endpos']==$endpos ) {
			    $nowlayout = $key;
			}
        }		
        if($nowlayout===false) {
            $this->flash("该layout标签不存在!", Request::referer());	 
        }	
		$newlayout = "<psp:layout   width='100px' ></psp:layout>";
		$content = $this->insert_str($this->pspLayout_out, $endspos+1, $newlayout);

		if (!file_exists($MviewFileName)) {
			$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
			$temp = explode(DS, $MviewFileName);
			array_pop( $temp );
			$tempFileDir = implode( DS.'' , $temp );
			get_dir($tempFileDir);
		}		
		if (!file_put_contents($MviewFileName, $content)) {
			$this->flash("操作失败!", Request::referer());
		}		
		$this->flash("操作成功!", Request::referer());		
	}	
	
	public function dellayout() {	
		$startpos = Request::$query['startpos'];		
		$endspos = Request::$query['endspos'];		
		$endpos = Request::$query['endpos'];	
		$pagename = Request::$query['pagename'];	
		$weburl = Request::$query['weburl'];		
		$webroot = Request::$query['webroot'];		
		$act = Request::$query['act'];			
		$app = Request::$query['app'];		
		$compeld = Request::$query['compeld'];	

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);

		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";

		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;			
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($viewFileName) && @filemtime($viewFileName) > @filemtime($MviewFileName)) {
				$nowFileName = $viewFileName;		   
			}else if( file_exists($MviewFileName) && @filemtime($viewFileName) < @filemtime($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
        if(empty($layoutdata))	{
            $this->flash("至少要有一个layout标签存在!", Request::referer());
        }	
		$nowlayout = false;
        foreach($layoutdata as $key => $value) {
            if( $value['startpos']==$startpos && $value['endspos']==$endspos && $value['endpos']==$endpos ) {
			    $nowlayout = $key;
			}
        }		
        if($nowlayout===false) {
            $this->flash("该layout标签不存在!", Request::referer());	 
        }	
		$newlayout = "<psp:layout   width='100px' ></psp:layout>";
		$content = $this->delete_str($this->pspLayout_out, $startpos ,$endpos+12 );
		if (!file_exists($MviewFileName)) {
			$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
			$temp = explode(DS, $MviewFileName);
			array_pop( $temp );
			$tempFileDir = implode( DS.'' , $temp );
			get_dir($tempFileDir);
		}		
		if (!file_put_contents($MviewFileName, $content)) {
			$this->flash("操作失败!", Request::referer());
		}		
		$this->flash("操作成功!", Request::referer());		
	}	
	
	public function addtags() {	
		$startpos = Request::$query['startpos'];		
		$endspos = Request::$query['endspos'];		
		$endpos = Request::$query['endpos'];	
		$pagename = Request::$query['pagename'];	
		$weburl = Request::$query['weburl'];		
		$webroot = Request::$query['webroot'];		
		$act = Request::$query['act'];			
		$app = Request::$query['app'];		
		$compeld = Request::$query['compeld'];	

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);

		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";

		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;			
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($viewFileName) && @filemtime($viewFileName) > @filemtime($MviewFileName)) {
				$nowFileName = $viewFileName;		   
			}else if( file_exists($MviewFileName) && @filemtime($viewFileName) < @filemtime($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
        if(empty($layoutdata))	{
            $this->flash("至少要有一个layout标签存在!", Request::referer());
        }	
		$nowlayout = false;
        foreach($layoutdata as $key => $value) {
            if( $value['startpos']==$startpos && $value['endspos']==$endspos && $value['endpos']==$endpos ) {
			    $nowlayout = $key;
			}
        }		
        if($nowlayout===false) {
            $this->flash("该layout标签不存在!", Request::referer());	 
        }
        $time = time();		
		$htmlname = 'layout'.$time;
		$newlayout = "<psp:tags     htmlname='".$htmlname."'  iconname='".$htmlname."' />";
		$content = $this->insert_str($this->pspLayout_out, $endspos+1, $newlayout);
		if (!file_exists($MviewFileName)) {
			$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
			$temp = explode(DS, $MviewFileName);
			array_pop( $temp );
			$tempFileDir = implode( DS.'' , $temp );
			get_dir($tempFileDir);
		}
		if (file_put_contents($MviewFileName, $content) && 
		file_put_contents($webroot1.DS.'tags'.DS.$htmlname.'.ps', 'Layout:'.$time)) {
			$this->flash("操作成功!", Request::referer());
		} else {     		
		    $this->flash("操作失败!", Request::referer());
        }			
	}	

	public function uplayout() {	
		$startpos = Request::$query['startpos'];		
		$endspos = Request::$query['endspos'];		
		$endpos = Request::$query['endpos'];	
		$pagename = Request::$query['pagename'];	
		$weburl = Request::$query['weburl'];		
		$webroot = Request::$query['webroot'];		
		$act = Request::$query['act'];			
		$app = Request::$query['app'];		
		$compeld = Request::$query['compeld'];	

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);

		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";

		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;			
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($viewFileName) && @filemtime($viewFileName) > @filemtime($MviewFileName)) {
				$nowFileName = $viewFileName;		   
			}else if( file_exists($MviewFileName) && @filemtime($viewFileName) < @filemtime($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
		
        if(empty($layoutdata))	{
            $this->flash("至少要有一个layout标签存在!", Request::referer());
        }	
		$nowlayout = false;
        foreach($layoutdata as $key => $value) {
            if( $value['startpos']==$startpos && $value['endspos']==$endspos && $value['endpos']==$endpos ) {
			    $nowlayout = $key;
			}
        }		
        if($nowlayout===false) {
            $this->flash("该layout标签不存在!", Request::referer());	 
        }
        $uplayoutkey = 0;
        $parentid = $layoutdata[$nowlayout]['parentid'];
        foreach($layoutdata as $key => $value) {
            if( $value['parentid']== $parentid ) {
			    if($key<$nowlayout && $key>$uplayoutkey){
				    $uplayoutkey = $key;	
                }					
			}
        }
        if($uplayoutkey==0) {
            $this->flash("已经到了顶层了，不能再向上移了!", Request::referer());	 
        }       
        $content = $this->pspLayout_out;		
		$getnowlayouthtml = substr($content,$layoutdata[$nowlayout]['startpos'],
		($layoutdata[$nowlayout]['endpos']+13)-$layoutdata[$nowlayout]['startpos']);

		$content = $this->delete_str($content, $layoutdata[$nowlayout]['startpos'] ,$layoutdata[$nowlayout]['endpos']+12 );
		
		$content = $this->insert_str($content, $layoutdata[$uplayoutkey]['startpos'], $getnowlayouthtml);	
	
		if (!file_exists($MviewFileName)) {
			$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
			$temp = explode(DS, $MviewFileName);
			array_pop( $temp );
			$tempFileDir = implode( DS.'' , $temp );
			get_dir($tempFileDir);
		}		
		if (!file_put_contents($MviewFileName, $content)) {
			$this->flash("操作失败!", Request::referer());
		}		
		$this->flash("操作成功!", Request::referer());		
	}		
	
	public function downlayout() {	
		$startpos = Request::$query['startpos'];		
		$endspos = Request::$query['endspos'];		
		$endpos = Request::$query['endpos'];	
		$pagename = Request::$query['pagename'];	
		$weburl = Request::$query['weburl'];		
		$webroot = Request::$query['webroot'];		
		$act = Request::$query['act'];			
		$app = Request::$query['app'];		
		$compeld = Request::$query['compeld'];	

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);

		$urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";

		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;			
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($viewFileName) && @filemtime($viewFileName) > @filemtime($MviewFileName)) {
				$nowFileName = $viewFileName;		   
			}else if( file_exists($MviewFileName) && @filemtime($viewFileName) < @filemtime($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_out = str_replace(array("\n"),array(''),$this->pspLayout_out );
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}		
		
		$layoutdata = $this->pspLayout_pares_data;
        if(empty($layoutdata))	{
            $this->flash("至少要有一个layout标签存在!", Request::referer());
        }	
		$nowlayout = false;
        foreach($layoutdata as $key => $value) {
            if( $value['startpos']==$startpos && $value['endspos']==$endspos && $value['endpos']==$endpos ) {
			    $nowlayout = $key;
			}
        }		
        if($nowlayout===false) {
            $this->flash("该layout标签不存在!", Request::referer());	 
        }	

        $downlayoutkey = $nowlayout;
        $parentid = $layoutdata[$nowlayout]['parentid'];
        foreach($layoutdata as $key => $value) {
            if( $value['parentid']== $parentid ) {
			    if($key>$downlayoutkey){
				    $downlayoutkey = $key;	
					break;
                }					
			}
        }
        if($downlayoutkey == $nowlayout) {
            $this->flash("已经到了底层了，不能再向下移了!", Request::referer());	 
        }  
        $content = $this->pspLayout_out;		
		$getnowlayouthtml = substr($content,$layoutdata[$nowlayout]['startpos'],
		($layoutdata[$nowlayout]['endpos']+13)-$layoutdata[$nowlayout]['startpos']);
		
		$content = $this->insert_str($content, $layoutdata[$downlayoutkey]['endpos']+13, $getnowlayouthtml);
	
		$content = $this->delete_str($content, $layoutdata[$nowlayout]['startpos']+1 ,$layoutdata[$nowlayout]['endpos']+13 );

		if (!file_exists($MviewFileName)) {
			$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
			$temp = explode(DS, $MviewFileName);
			array_pop( $temp );
			$tempFileDir = implode( DS.'' , $temp );	
			get_dir($tempFileDir);
		}		
		if (!file_put_contents($MviewFileName, $content)) {
			$this->flash("操作失败!", Request::referer());
		}		
		$this->flash("操作成功!", Request::referer());		
	}	
	
	/**
    指定位置删除字符
	*/
    public function delete_str($str, $start ,$offset ){
		$newstr = '';
		for ($i = 0; $i < strlen($str); $i++){
		    if($i>=$start && $i<=$offset) continue;
			$newstr.=$str{$i};
		}
		return $newstr;
	}	
	
	/**
	* 在指定位置插入指定字符串
	* @param type $str 预准备字符串
	* @param type $offset    位置偏移量
	* @param type $input    插入的字符串
	* @return type        返回新的字符串
	* @author abcls.cn 编程路上
	echo insert_str('123456789', array(2,8), '—');	
	*/
    public function insert_str($str, $offset, $input){
		$newstr = '';
		for ($i = 0; $i < strlen($str); $i++){
			if (is_array($offset)) {//如果插入是多个位置
				foreach ($offset as $v)	{
					if ($i == $v) $newstr.=$input;
				}
			}else {//直接是一个位置偏移量
				if ($i == $offset) $newstr.=$input;
			}
			$newstr.=$str{$i};
		}
		return $newstr;
	}

	
    public function PspLayoutParse($TemplateCode) {	
		$layouts = array();
		$layout_s = array();
		$layout_e = array();	
		$TemplateLen = strlen(trim($TemplateCode)); 
		$HtmlCode = ""; 
		$SearchTag = "<psp:layout";//标记的起始
		$SearchTagSE = ">";//标记的属性结束
		$SearchTagE = "</psp:layout>";//标记的范围结束	
		
		$StartPos = 0;//在循环中记录当前已经处理到的位置
		$count=0;
		$MyPos = 0;  
		$MyPosSE = 0; 
		$MyPosE = 0; 	
		$FindEnd = false; 
		$TagName = ""; 	
		While (!$FindEnd && $TemplateLen > 0) { 
			//找到下一个标记的起始位置
			$MyPos = strpos($TemplateCode,$SearchTag,$StartPos);
			if($MyPos ===false) {  
				$FindEnd = true;  
			}else{	
				$count++;	
				$MyPosSE = strpos($TemplateCode,$SearchTagSE,$MyPos);			
				$layout_s[$count]['id'] = $count;			
				$layout_s[$count]['startpos'] = $MyPos;	
				$layout_s[$count]['endspos'] = $MyPosSE;			
				$TagName = substr($TemplateCode, $MyPos + 11, $MyPosSE - $MyPos - 11); 	
				$arrTagName = explode(" ",$TagName);            			
				for($i=0;$i<count($arrTagName);$i++) { 
					$TempTagName = $arrTagName[$i];  
					//根据=再次进行分割
					$arrParamter = explode("=",$TempTagName); 
					if(isset($arrParamter[0]) && isset($arrParamter[1]) && !empty($arrParamter[0]) && !empty($arrParamter[1])) {
						$layout_s[$count]['style'][$arrParamter[0]] = trim($arrParamter[1],"'");
					}
				}
				$StartPos = $MyPosSE +1;			
			}
		}
		$StartPos = 0;	
		$count=0;
		$MyPos = 0;  
		$MyPosSE = 0; 
		$MyPosE = 0; 	
		$FindEnd = false; 
		$TagName = ""; 	
		While (!$FindEnd && $TemplateLen > 0) { 
			//找到下一个标记的起始位置
			$MyPos = strpos($TemplateCode,$SearchTagE,$StartPos);
			if($MyPos ===false) {  
				$FindEnd = true;  
			}else{	
				$count++;				
				$layout_e[$count] = $MyPos;
				$StartPos = $MyPos + 13;			
			}
		}	
		if(count($layout_e) != count($layout_s)){//假如<psp:layout的数量和</psp:layout>的数量不同则退出
			return false;
		}
		foreach($layout_e as $endpos) {
			$num = 0;		
			foreach($layout_s as $k=> $pos) {		
				$kk = $this->findnextkey($pos,$layout_s);		
				if($endpos>=$pos['endspos'] && ( ( $kk && $endpos < $layout_s[$kk]['endspos']) || $kk===false )) {
					$layouts[$k] = $pos;
					$layouts[$k]['endpos'] = $endpos;
					$layouts[$k]['parentid'] = $this->findprevkey($pos,$layout_s);				
					unset($layout_s[$k]);				
				}			
			}
		}
		return $layouts;
    }


    public function findnextkey($finddata,$data){
		if(empty($finddata))return false;
		if(empty($data))return false;
		$nextdata = false;
		reset($data);
		while($result=current($data)){
			if($finddata['startpos']==$result['startpos'] && $finddata['endspos']==$result['endspos'] ) {
				$nextdata = next($data);
				break;		
			}	
			next($data);   
		}
		if($nextdata) {
			foreach($data as $key => $value) {
				if($nextdata['startpos']==$value['startpos'] && $nextdata['endspos']==$value['endspos'] ) {
					return $key;
				}
			} 
		}	
		return false;
    }

    public function findprevkey($finddata,$data){
		if(empty($finddata))return false;
		if(empty($data))return false;
		$nextdata = 0;
		reset($data);
		while($result=current($data)){
			if($finddata['startpos']==$result['startpos'] && $finddata['endspos']==$result['endspos'] ) {
				$nextdata = prev($data);
				break;
			}	
			next($data);   
		}
		if($nextdata) {
			foreach($data as $key => $value) {
				if($nextdata['startpos']==$value['startpos'] && $nextdata['endspos']==$value['endspos'] ) {
					return $key;
				}
			} 
		}
		if($nextdata===false)return 0;	
		return false;
    }
	
	
 	public function _urldecode($msg){
		$msg = urldecode($msg);		
		$msg = str_replace(';gang;','-',$msg);	
		return $msg;
	}
	public function _urlencode($msg){
		$msg = str_replace('-',';gang;',$msg);
		$msg = urlencode($msg);		
		return $msg;
	}
	public function filtrate($msg){
		$msg = str_replace(';deng;','=',$msg);
		$msg = str_replace(';kong;',' ',$msg);
		$msg = str_replace(';shuang;','"',$msg);
		$msg = str_replace(';dang;',"'",$msg);
		$msg = str_replace(';and;',"&&",$msg);		
		$msg = str_replace(';or;',"||",$msg);
		$msg = str_replace(';gang;',"-",$msg);				
		return $msg;
	} 	
/*	
	public function layoutupdata_old() {
	
		$pagename = Request::get_args("pagename");	
		$weburl = Request::get_args("weburl");		
		$webroot = Request::get_args("webroot");		
		$act = Request::get_args("act");			
		$app = Request::get_args("app");		
		$compeld = Request::get_args("compeld");

		$pagename= $this->_urldecode($pagename);
		$pagename1 = $pagename;				
		$pagename= $this->_urlencode($pagename);		

		$compeld= $this->_urldecode($compeld);
		$compeld1 = $compeld;				
		$compeld= $this->_urlencode($compeld);
		
		$webroot= $this->_urldecode($webroot);
		$webroot1 = $webroot;				
		$webroot= $this->_urlencode($webroot);		
	
		$weburl= $this->_urldecode($weburl);
		$fromurl = $weburl;				
		$weburl= $this->_urlencode($weburl);
        $urlstr="&weburl={$weburl}&compeld={$compeld}&app={$app}&act={$act}&webroot={$webroot}&pagename={$pagename}";	
		$viewFileName = $webroot1.DS.$pagename1;			
		$MviewFileName = $compeld1;		
		if($this->pspLayout_out==null) {
			$nowFileName = $viewFileName;			
			if( file_exists($MviewFileName)) {
				$nowFileName = $MviewFileName;		   
			}			
			$this->pspLayout_out = '';
			$this->pspLayout_out = file_get_contents($nowFileName);
			$this->pspLayout_pares_data = $this->PspLayoutParse($this->pspLayout_out);
		}			
		$layoutdata = $this->pspLayout_pares_data;
		$toplayout = array();
		$toplayoutstr ='';
		$num=0;
		foreach($layoutdata as $key => $value) {
			if( $value['parentid']==0) {
				$toplayout[$num] = $key;
				$toplayoutstr .= $key.',';
				$num++;
			}
		}		

		$order = Request::get_args("order");
		$itemid = trim(trim(Request::get_args("id")),',');		
		if (!empty ($itemid)) {

			$newlayout = explode(",",$itemid);
			foreach($newlayout as $k => $v) {
				if(!in_array($v,$toplayout)){
					unset($newlayout[$k]);
				}
			}
			
			$content = $this->pspLayout_out;
			$newcontent = $content;				
			$gettoplayouthtml = array();
			$countkey = 0;

			foreach($toplayout as $v) {	
				$gettoplayouthtml[$v] = substr($content,$layoutdata[$v]['startpos'], 
					($layoutdata[$v]['endpos']+13)-$layoutdata[$v]['startpos'] ); 
					
				$strpad = str_pad($v, 4, "0", STR_PAD_LEFT)	;
				$startpos = $layoutdata[$v]['startpos'] - $countkey;
				$endpos = $layoutdata[$v]['endpos'] - $countkey;
				$newcontent = $this->delete_str($newcontent, $startpos ,$endpos+12 );	
				$newcontent = $this->insert_str($newcontent, $startpos, '<psp-layout-'.$strpad.'/>');				
				$countkey += (($endpos+13) - $startpos) -18;					
			}	
			foreach($newlayout as $k => $v) {
				$strpad = str_pad($toplayout[$k], 4, "0", STR_PAD_LEFT)	;
				$newcontent = str_replace( '<psp-layout-'.$strpad.'/>', $gettoplayouthtml[$v] ,$newcontent);
				
			$query = array('sortlist'=>array('sort'=>$itemid,'old'=>$toplayoutstr,'layout'=>'<psp-layout-'.$strpad.'/>','html'=>$gettoplayouthtml[$v]));        	
			$sql ='[@@:UM sortlist][@@:QU save]';
			POP::exe($sql,$query);	
			
			}

			if (!file_exists($MviewFileName)) {
				$MviewFileName = str_replace(array('\\','/'), DS ,$MviewFileName);
				$temp = explode(DS, $MviewFileName);
				array_pop( $temp );
				$tempFileDir = implode( DS.'' , $temp );
				get_dir($tempFileDir);
			}	
		
			if (!file_put_contents($MviewFileName, $newcontent)) {
				echo "none";
			}else {
				echo $itemid;
			}

		}
		exit;
    }		
*/		
}
?>